#include "resourcelistwidget.h"
#include <QDragEnterEvent>
#include <QDir>
#include <QVBoxLayout>
#include <QDebug>
#include <QApplication>
#include "filemanager.h"
#include "eventcenter.h"
ResourceListWidget::ResourceListWidget(QWidget* parent)
{
    this->setParent(parent);
    Init();
    this->setDragEnabled(true);
}

ResourceListWidget::~ResourceListWidget()
{

}

void ResourceListWidget::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
    qDebug()<<"列表窗口拖拽";
}

void ResourceListWidget::SetResPath(QString path)
{
    this->resPath = path;
}

void ResourceListWidget::SetCurPath(QString path)
{
    this->curPath = path;
}

QString ResourceListWidget::GetResPath()
{
    return this->resPath;
}

QString ResourceListWidget::GetCurPath()
{
    return this->curPath;
}

QString ResourceListWidget::GetCurItemPath()
{
    return FileManager::Instance().curPath + "/" +this->currentItem()->text();
}

void ResourceListWidget::Init()
{
    QSize iconSize = QSize(100,100);
    this->setIconSize(iconSize);
    this->setViewMode(QListView::IconMode);
    this->setFlow(QListView::LeftToRight);
    this->setResizeMode(QListView::Adjust);
    this->setMovement(QListView::Static);

    RefreshList(QDir::currentPath());//显示工作目录

    connect(this,&QListWidget::itemClicked,&EventCenter::Instance(),[&](QListWidgetItem* item){
        QWidget* parent = this->parentWidget();
        if(parent && !parent->windowTitle().isEmpty())
            emit EventCenter::Instance().ClickFileItem(item);
    });

    connect(this,&QListWidget::itemDoubleClicked,this,[&](QListWidgetItem* item){
        emit EventCenter::Instance().DoubleClickFileItem(item);
        ClickItemSlot(item);
    });
    connect(&EventCenter::Instance(),&EventCenter::RefreshFileList,this,[=](QString& path){
       RefreshList(path);
    });
}

void ResourceListWidget::RefreshList(QString path)
{
    this->clear();
    //添加返回按钮
    if(path.length() > FileManager::Instance().GetRootPath().length())
    {
        AddIconItem(":/res/icon/back.png","上级目录");
    }

    FileManager::Instance().curPath = path;
    QDir * dir = new QDir(path);
    QStringList filter;
    QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter,QDir::Dirs|QDir::Files|QDir::Readable,QDir::DirsFirst));
    for (int i = 0;i < fileInfo->count();i++)
    {
        QFileInfo fi = fileInfo->at(i);
        if(fi.fileName() == "." || fi.fileName() == "..")
            continue;
        AddFileItem(fi);
    }
}

void ResourceListWidget::AddIconItem(QString iconPath,QString name)
{
    float iconTextHeight = 30.0f;
    QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(iconPath)),name);
    item->setSizeHint(QSize(iconSize.width(),iconSize.height()+iconTextHeight));
    item->setText(name);
    item->setToolTip(name);
    item->setTextAlignment(Qt::AlignBottom|Qt::AlignJustify|Qt::AlignHCenter);
    this->addItem(item);
}

void ResourceListWidget::AddFileItem(QFileInfo& fi)
{
    if(!(fi.isDir() || fi.isFile()))
        return;
    if(fi.isFile())
    {
        int fileType = FileManager::GetFileType(fi.fileName());
        switch (fileType)
        {
            case FileManager::IMAGE:
                AddIconItem(fi.filePath(),fi.fileName());
                break;
            case FileManager::SOUND:
                AddIconItem(":/res/icon/sound.png",fi.fileName());
                break;
            case FileManager::VIDEO:
                AddIconItem(":/res/icon/run.png",fi.fileName());
                break;
            default:
                break;
        }
    }
    else
    {
        AddIconItem(":/res/icon/folder.png",fi.fileName());
    }
}

void ResourceListWidget::ClickItemSlot(QListWidgetItem *item)
{
    QString path = FileManager::Instance().curPath + "/" + item->text();
    if(item->text() == "上级目录")
    {
        QFileInfo fi(FileManager::Instance().curPath);
        QString filePath = fi.path();
        emit EventCenter::Instance().RefreshFileList(filePath);
    }else if(QFileInfo(path).isDir())
    {
        emit EventCenter::Instance().RefreshFileList(path);
    }
}
